﻿using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Globalization;
using System.Collections.Specialized;
using System.Runtime.InteropServices;

namespace HongShijin.PrintActiveX
{
	static class Common
	{
		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new CFormPrintHTML());
		}

		public static float ParseFloat(object value)
		{
			return ParseFloat(value, 0);
		}
		public static float ParseFloat(object value, float defaultValue)
		{
			float f = defaultValue;
			if (value == null)
				return f;

			string t = value.ToString();
			if (value is XmlAttribute)
				t = ((XmlAttribute)value).Value;
			if (float.TryParse(t, out f))
				return f;

			return defaultValue;
		}

		public static int ParseInt32(object value, int defaultValue)
		{
			int i = defaultValue;
			if (value == null)
				return i;

			string t = value.ToString();
			if (value is XmlAttribute)
				t = ((XmlAttribute)value).Value;

			if (int.TryParse(t, out i))
				return i;

			return defaultValue;
		}

		public static Font ParseFont(object value, float size)
		{
			return ParseFont(value, size, FontStyle.Regular);
		}
		public static Font ParseFont(object value, float size, FontStyle style)
		{
			if (size <= 0)
				size = 12;
			Font font = new Font(FontFamily.Families[0], size, style);
			if (value == null)
				return font;

			string t = value.ToString();
			if (value is XmlAttribute)
				t = ((XmlAttribute)value).Value;
			if (string.IsNullOrEmpty(t))
				return font;

			try
			{
				return new Font(value.ToString(), size, style);
			}
			catch// (Exception x)
			{
				return font;
			}
		}

		public static Color ParseColor(object value)
		{
			return ParseColor(value, Color.Black);
		}
		public static Color ParseColor(object value, Color defaultValue)
		{
			if (value == null)
				return defaultValue;

			string t = value.ToString();
			if (value is XmlAttribute)
				t = ((XmlAttribute)value).Value;
			if (string.IsNullOrEmpty(t))
				return defaultValue;

			try
			{

				if (t[0] == '#')
				{
					if (t.Length > 4)
						return Color.FromArgb(int.Parse(string.Empty + t[1] + t[2], NumberStyles.HexNumber), int.Parse(string.Empty + t[3] + t[4], NumberStyles.HexNumber), int.Parse(string.Empty + t[5] + t[6], NumberStyles.HexNumber));

					return Color.FromArgb(int.Parse(string.Empty + t[1] + t[1], NumberStyles.HexNumber), int.Parse(string.Empty + t[2] + t[2], NumberStyles.HexNumber), int.Parse(string.Empty + t[3] + t[3], NumberStyles.HexNumber));
				}

				return Color.FromName(t);
			}
			catch// (Exception x)
			{
				return defaultValue;
			}
		}

		public static string GetFile(string name)
		{
			string t = null;
			Assembly assembly = Assembly.GetEntryAssembly();
			if (assembly != null)
				t = GetFile(assembly.Location, name);
			if (!string.IsNullOrEmpty(t))
				return t;

			assembly = Assembly.GetExecutingAssembly();
			if (assembly != null)
				t = GetFile(assembly.Location, name);
			if (!string.IsNullOrEmpty(t))
				return t;

			assembly = Assembly.GetCallingAssembly();
			if (assembly != null)
				t = GetFile(assembly.Location, name);
			if (!string.IsNullOrEmpty(t))
				return t;

			return null;
		}

		public static string GetFile(string path, string name)
		{
			DirectoryInfo dir = new DirectoryInfo(path);
			while (dir.FullName != dir.Root.FullName)
			{
				path = Path.Combine(dir.FullName, name);
				if (File.Exists(path))
					return path;

				dir = dir.Parent;
			}
			return null;
		}
	}

	public enum EItemType : int
	{
		Unknow = 0,
		Pager = 1,
		Setting = 2,
		Image = 3,
		Text = 4,
		Line = 5,
		Arc = 6,
		Rectangle = 7
	}

	#region "ObjectSafety" interface for "internet explorer"

	[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
	[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
	public interface IObjectSafety
	{
		[PreserveSig]
		int GetInterfaceSafetyOptions(
			ref Guid riid,
			[MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions,
			[MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

		[PreserveSig]
		int SetInterfaceSafetyOptions(
			ref Guid riid,
			[MarshalAs(UnmanagedType.U4)] int dwOptionsSetMask,
			[MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
	}

	public class CObjectSafety : IObjectSafety
	{
		private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
		private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
		private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
		private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
		private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

		private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
		private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
		private const int S_OK = 0;
		private const int E_FAIL = unchecked((int)0x80004005);
		private const int E_NOINTERFACE = unchecked((int)0x80004002);

		private bool _fSafeForScripting = true;
		private bool _fSafeForInitializing = true;

		public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
		{
			//pdwSupportedOptions = 1; 
			//pdwEnabledOptions = 2; 
			int Rslt = E_FAIL;
			string strGUID = riid.ToString("B");
			pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
			switch (strGUID)
			{
				case _IID_IDispatch:
				case _IID_IDispatchEx:
					Rslt = S_OK;
					pdwEnabledOptions = 0;
					if (_fSafeForScripting == true)
						pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
					break;
				case _IID_IPersistStorage:
				case _IID_IPersistStream:
				case _IID_IPersistPropertyBag:
					Rslt = S_OK;
					pdwEnabledOptions = 0;
					if (_fSafeForInitializing == true)
						pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
					break;
				default:
					Rslt = E_NOINTERFACE;
					break;
			}

			return Rslt;
		}

		public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
		{
			int Rslt = E_FAIL;

			string strGUID = riid.ToString("B");
			switch (strGUID)
			{
				case _IID_IDispatch:
				case _IID_IDispatchEx:
					if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
						Rslt = S_OK;
					break;
				case _IID_IPersistStorage:
				case _IID_IPersistStream:
				case _IID_IPersistPropertyBag:
					if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
						Rslt = S_OK;
					break;
				default:
					Rslt = E_NOINTERFACE;
					break;
			}

			return Rslt;
		}
	}

	#endregion

	#region 控件公开对象:5.0

	/// <!--
	/// "1e014000-96ea-456f-8595-9ceaa0f11516"
	/// "06e7234a-3189-4f5c-b411-e7f4e146005a"
	/// "d888e032-c797-4f0a-baea-be08cb80e61b"
	/// "9f061874-bf6a-4d56-87e5-dce82390b2d5"
	/// "a5511731-7e9b-4b30-a136-0c2c98cfd732"
	/// "f1c9ff1c-c23c-430d-b2ff-433add51bc38"
	/// "14eb9292-5cce-42ea-9766-db816166ec10"
	/// "6076e95e-8bb2-4ba2-94fb-4393a913ad04"
	/// "7692e68f-2e4f-40f4-b50f-bba0565ca8ba"
	/// "54e60a42-6b08-4c00-a967-5fbe2b4263eb"
	/// "71e5a2a4-ce78-425f-bf7f-8361b5778a41"
	/// "bbdd7f0f-3569-43fd-b689-044c757a63c9"
	/// "71b5bc15-abe4-4606-904d-967825487762"
	/// "e9e56c4a-5a2a-4dec-98ae-4adb0f9c05de"
	/// "b79c3808-b2e0-40ef-ac7b-5a6662af3a65"
	/// "c1fa8beb-b8bf-4358-9529-71f0d72bf200"
	/// "3026cd10-1ec0-4a7f-a02c-ff9450a7e4e2"
	/// "47d432a0-dfef-4e09-9c17-3009ae30362a"
	/// "919c390e-7b88-4214-af83-1a42b6648b80"
	/// "b74d4b16-a22a-401b-a487-676b398c6218"
	/// "25e5658f-8268-4aa9-8059-298ec2cabc8b"
	/// "36de49d7-f2bb-467a-a067-55174707b5f0"
	/// -->
	
	[ComVisible(true)]
	[Guid("194b8426-c2e5-4b3b-86dd-aead6ad58549")]
	public class ActiveX_05_0005 : PrintMechanically, IObjectSafety
	{
		private IObjectSafety _ObjectSafety;

		public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
		{
			if (this._ObjectSafety == null)
				this._ObjectSafety = new CObjectSafety();
			return this._ObjectSafety.GetInterfaceSafetyOptions(ref riid, ref pdwSupportedOptions, ref pdwEnabledOptions);
		}

		public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
		{
			if (this._ObjectSafety == null)
				this._ObjectSafety = new CObjectSafety();
			return this._ObjectSafety.SetInterfaceSafetyOptions(ref riid, dwOptionSetMask, dwEnabledOptions);
		}
	}
	
	[ComVisible(true)]
	[Guid("6020bd2c-fb66-4014-b6a5-1d97d77d4ac1")]
	public class ActiveX_05_0000 : PrintMechanically, IObjectSafety
	{
		private IObjectSafety _ObjectSafety;

		public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
		{
			if (this._ObjectSafety == null)
				this._ObjectSafety = new CObjectSafety();
			return this._ObjectSafety.GetInterfaceSafetyOptions(ref riid, ref pdwSupportedOptions, ref pdwEnabledOptions);
		}

		public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
		{
			if (this._ObjectSafety == null)
				this._ObjectSafety = new CObjectSafety();
			return this._ObjectSafety.SetInterfaceSafetyOptions(ref riid, dwOptionSetMask, dwEnabledOptions);
		}
	}

	[ComVisible(true)]
	[Guid("e66af9b5-0bf1-4047-9b6d-6bad1b5e48d1")]
	public class ActiveX_01_0000 : PrintMechanically, IObjectSafety
	{
		private IObjectSafety _ObjectSafety;

		public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
		{
			if (this._ObjectSafety == null)
				this._ObjectSafety = new CObjectSafety();
			return this._ObjectSafety.GetInterfaceSafetyOptions(ref riid, ref pdwSupportedOptions, ref pdwEnabledOptions);
		}

		public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
		{
			if (this._ObjectSafety == null)
				this._ObjectSafety = new CObjectSafety();
			return this._ObjectSafety.SetInterfaceSafetyOptions(ref riid, dwOptionSetMask, dwEnabledOptions);
		}
	}

	#endregion
}